package us.icitap.cm.ui.policecase;

import java.util.List;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppEntity;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.EntityTypeEnum;
import us.icitap.cm.business.entities.Organization;
import us.icitap.cm.listeners.AppEntityListener;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustTextArea;
import us.icitap.cm.ui.custom.CustTextField;
import us.icitap.cm.ui.custom.CustomFormLayout;
import us.icitap.cm.ui.shared.AppEntityLayout;
import us.icitap.cm.utils.ProjectLists;
import us.icitap.utils.Result;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;

public class OrganizationDataLayout extends AppEntityLayout{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1728677973922071325L;

	private Organization organization;
	protected AppEntityListener listener;
	private final CustBeanFieldGroup<Organization> binder = new CustBeanFieldGroup<Organization>(Organization.class);
	
	final CustComboBox orgClassSelect = new CustComboBox("Klasifikimi i organizates");
	final CustComboBox orgTypeSelect = new CustComboBox("Tipi i organizates");
	final CustTextField nameTextField = new CustTextField("Emri");
	final CustTextField memberNoTextField = new CustTextField("Nr. i anetareve");
	final CustTextArea notesTextArea = new CustTextArea("Shenime");
	
	final CustomFormLayout cfl = new CustomFormLayout("450px", "Organizate e perfshire ne ngjarje", "Reset", "Ruaj");
	
	boolean editableVisible = false;
	boolean readOnlyVisible = false;

	final TabSheet tabSheet = new TabSheet();
	
	public OrganizationDataLayout() {
		this.setMargin(true);

		
		
		FormLayout fl = new FormLayout();
		fl.addStyleName("cmLayout");
		fl.setMargin(true);
		fl.addComponent(this.orgClassSelect);
		fl.addComponent(this.orgTypeSelect);
		fl.addComponent(this.nameTextField);
		fl.addComponent(this.memberNoTextField);
		fl.addComponent(this.notesTextArea);

		this.memberNoTextField.setWidth("6em");
		tabSheet.addTab(fl, "Te dhenat");
	
		
		cfl.addComponent(tabSheet);
		cfl.addFooter();
		
		orgClassSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.OrgClassification, false)));
	
		orgClassSelect.setImmediate(true);
		
		orgClassSelect.addValueChangeListener(new ValueChangeListener() {
			
			@Override
			public void valueChange(ValueChangeEvent event) {
				orgTypeSelect.removeAllItems();
				if(orgClassSelect.getValue() != null){
					AppList parent = (AppList) orgClassSelect.getValue();
					Result<List<AppList>> result = CaseManagementUI.appStatelessBean.getPersistenceBean().getAppListChildren(parent);
					if(result.isSucceeded() && result.getObject() != null && !result.getObject().isEmpty()){
						orgTypeSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, result.getObject()));
					}
				}
				
			}
		});
		
		
		
		cfl.addRightClickListener(new ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				try {
					binder.commit();
					if(OrganizationDataLayout.this.listener != null){
						if(organization.getAppEntity().isNewRecord()){
							OrganizationDataLayout.this.listener.addAppEntityToReport(organization.getAppEntity());
						}
						else
							if(!oldAppEntity.getOrganization().myEquals(organization)){
								organization.getAppEntity().setRecordUser(CaseManagementUI.getUser().getUsername());
								OrganizationDataLayout.this.listener.updateAppEntityIntoReport(oldAppEntity, organization.getAppEntity());
							}
							else
							{
//								oldAppEntity.replaceEntityReportAssociationList(organization.getAppEntity().getReportAssociationList());
								OrganizationDataLayout.this.listener.refresh(oldAppEntity);
							}
						
						addAppEntity();
					}
				} catch (CommitException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		binder.bind(this.orgClassSelect, "orgClassification");
		binder.bind(this.orgTypeSelect, "organizationType");
		binder.bind(this.nameTextField, "name");
		binder.bind(this.memberNoTextField, "memberNo");
		binder.bind(this.notesTextArea, "notes");
		
	}

	

	public void showEditableForm(){
		if(editableVisible) return ;
		
		this.removeAllComponents();
		this.addComponent(cfl);
		
		editableVisible = true;
		readOnlyVisible = false;
	}
	

	public void showReadOnlyForm(){
		if(readOnlyVisible) return;
		this.removeAllComponents();
		VerticalLayout vl = ViewCaseLayout.showOrganization(organization);
		vl.setCaption(EntityTypeEnum.ORGANIZATION.getCaption());
		this.addComponent(vl);
		editableVisible = false;
		readOnlyVisible = true;
	}
	
	@Override
	public void addAppEntity() {
		this.tabSheet.setSelectedTab(0);
		AppEntity appEntity = new AppEntity(CaseManagementUI.getUser().getUsername(), EntityTypeEnum.ORGANIZATION);
		organization = appEntity.getOrganization();
		binder.setItemDataSource(organization);

		this.showEditableForm();
	}
	
	@Override
	public void addAppEntityListener(AppEntityListener listener) {
		this.listener = listener;
		
	}

	private AppEntity oldAppEntity;
	

	@Override
	public void loadAppEntity(AppEntity appEntity, boolean editable) {

		oldAppEntity = appEntity;
		
		AppEntity newAppEntity = new AppEntity(appEntity);
		
		this.organization = newAppEntity.getOrganization();
		binder.setItemDataSource(organization);
		if(editable) 
			this.showEditableForm();
		
		else
			this.showReadOnlyForm();
	}

}
